/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.util;

import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

/**
 *
 */
public class Util {

    public static String readFile(String path) throws IOException {
        return readFile(new File(path));
    }

    public static String readFile(File f) throws IOException {
        FileInputStream  in = null;
        try {
            in = new FileInputStream(f);
            return readFile(in);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    public static String readFile(InputStream in) throws IOException {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = in.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }

    public static String readFileMapped(String path) throws IOException {
        return readFileMapped(new File(path));
    }

    public static String readFileMapped(File f) throws IOException {
        FileInputStream stream = new FileInputStream(f);
        FileChannel fc = null;
        try {
            fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            /* Instead of using default, pass in a decoder. */
            return Charset.defaultCharset().decode(bb).toString();
        }
        finally {
            if (fc != null) {
                fc.close();
            }
            stream.close();
        }
    }


    public static String exceptionStackTraceToString(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();        
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static String emptyAsNull(String str) {
        if (str == null || str.trim().length() == 0) {
            return null;
        }
        return str;
    }

    public static String indent(int i) {
        String str = "";
        while (i-- > 0) {
            str += "  ";
        }
        return str;
    }

    public static Throwable findCause(Throwable e) {
        Throwable cause;
        do {
            cause = e.getCause();
            if (cause != null) {
                e = cause;
            }
        } while (cause != null);
        return e;
    }
}
